export = class Matcher {
    out: Record<string, any> = {}

    constructor(public html: string) {
        // this.html = html
    }

    matchVar(key) {
        const result = this.html.match(new RegExp(`var ${key} = '(.+?)';`))
        if (result) {
            this.out[key] = result[1]
        }
        return this
    }

    matchDataVar(key) {
        const varName = this.html.match(new RegExp(`'${key}':'?(\\w+)'?,`))
        if (varName) {
            const result = this.html.match(new RegExp(`var ${varName[1]} = '(.+?)';`))
            if (result) {
                this.out[key] = result[1]
            }
        }
        return this
    }

    matchData(key) {
        const result = this.html.match(new RegExp(`'${key}':'?(\\w+)'?,`))
        if (result) {
            this.out[key] = result[1]
        }
        return this
    }

    matchObject(key) {
        const result = this.html.match(new RegExp(`${key} : '(.+?)',`))
        if (result) {
            this.out[key] = result[1]
        }
        return this
    }

    // 文件，带密码
    matchPwdFile(key) {
        // type : 'post',
        // url : '/ajaxm.php',
        // data : 'action=downprocess&sign=BmABPw4_aDz4IAQA_aV2dRbVozAjVQPwExBTUHNVI2AzQAJgAjXDxXMglpAWcGZgI2Uz4CNlc_bAjYBMA_c_c&p='+pwd,
        // dataType : 'json',
        const result = this.html.match(new RegExp(`${key} ?: ?'(.*?)'`))
        if (result) {
            this.out[key] = result[1]
        }
        return this
    }

    matchIframe(key = 'iframe') {
        return this.match(key, '<iframe.*src="(\\/fn\\?\\w{5,})" ')
    }

    matchTitle(key = 'title') {
        const result = this.html.match(/<title>(.*?)<\/title>/)
        if (result) {
            this.out[key] = result[1].replace(' - 蓝奏云', '')
        }
        return this
    }

    matchSecondVerify() {
        const result = this.html.match(new RegExp(`\tdata : (.*),`))
        if (result) {
            const objStr = result[1].replace(':el', ':1')
            const obj = eval(`(${objStr})`)
            this.out['data'] = obj
        }
        return this
    }

    match(key, pattern) {
        const result = this.html.match(new RegExp(pattern))
        if (result) {
            this.out[key] = result[1]
        }
        return this
    }

    done() {
        return this.out
    }
}

// module.exports = Matcher
